const WebSocket = require('ws')
const ws = new WebSocket.Server({ port: 3000 })
let id = 1

function getMockData(objectId = 8000005) {
  const objData = {}
  const labelP = (Math.random() * 10).toFixed(3)
  const labelQ = (Math.random() * 10).toFixed(3)
  const labelI = (Math.random() * 10).toFixed(3)
  objData.id = id++
  objData.objectId = objectId
  objData.labelP = labelP
  objData.labelQ = labelQ
  objData.labelI = labelI
  return objData
}

ws.on('connection', ws => {
  console.log('连接成功')
  ws.on('message', () => {
    setInterval(() => {
      const objData = getMockData()
      ws.send(JSON.stringify(objData))
    }, 1000)
  })

  ws.on('close', () => {
    console.log('连接断开')
  })
})